﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace 低频天线阵列相控阵信号发生系统SmartFASK12.Controls
{
    public class MData
    {
        /// <summary>
        /// 最原始数据X,也就是时间
        /// </summary>
        public List<double> OrigXValues = new List<double>();
        /// <summary>
        /// 最原始数据Y
        /// </summary>
        public List<double> OrigYValues = new List<double>();

        /// <summary>
        /// 有效原始数据X,也就是时间
        /// </summary>
        public List<double> XValues = new List<double>();
        /// <summary>
        /// 有效原始数据Y
        /// </summary>
        public List<double> YValues = new List<double>();
        /// <summary>
        /// 采样率
        /// </summary>
        public int SampleFre = 0;

        /// <summary>
        /// 频率1带通滤波后的数据
        /// </summary>
        public List<double> DataFre1 = new List<double>();
        /// <summary>
        /// 频率2带通滤波后的数据
        /// </summary>
        public List<double> DataFre2 = new List<double>();

        /// <summary>
        /// 
        /// </summary>
        public List<int> JudgeResult = new List<int>();

        public Dictionary<int, int> JudgePoints = new Dictionary<int, int>();

        public List<int> FinalResult = new List<int>();
        public int FinalShowCount = 0;

        public int MinShowFre = 300;

        public List<int[]> JudgeShowPointsPart = new List<int[]>();
        public List<int[]> JudgeShowPointsFull = new List<int[]>();

        public void Clear()
        {
            XValues.Clear();
            YValues.Clear();

            DataFre1.Clear();
            DataFre2.Clear();

            JudgeResult.Clear();

            FinalResult.Clear();

            JudgePoints.Clear();
        }

        public void CutZero(int num=10, double mid =0,double amp=0.05)
        {

            for (int i = 0; i < YValues.Count - num; i++)
            {
                if (Math.Abs(YValues[i] - mid) >= amp)
                {
                    
                    if(i>=num)
                    {
                        YValues.RemoveRange(0, i);
                        XValues.RemoveRange(0, i);
                    }

                    break;
                }
            }

            for (int i = YValues.Count-1; i>=num; i--)
            {
                if (Math.Abs(YValues[i] - mid) >= amp)
                {

                    if (i<YValues.Count-num)
                    {
                        YValues.RemoveRange(i+1, YValues.Count-i-1);
                        XValues.RemoveRange(i + 1, XValues.Count - i - 1);
                    }

                    break;
                }
            }

        }

        public void ExportData(string filename)
        {
            if(File.Exists(filename))
            {
                File.Delete(filename);
            }

            using (StreamWriter sw = File.AppendText(filename))
            {
                sw.WriteLine("时刻,频率1包络,频率2包络,幅度判别");

                for(int i=0;i<DataFre1.Count;i++)
                {
                    string str = string.Format("{0},{1},{2},{3}", XValues[i], DataFre1[i], DataFre2[i], JudgeResult[i]);
                    sw.WriteLine(str);
                }
            }
        }

        public void Judgement(int shift,int discnt,int showcnt,bool isgyh=false)
        {
            JudgeResult.Clear();

            FinalResult.Clear();

            JudgePoints.Clear();

            JudgeShowPointsFull.Clear();
            JudgeShowPointsPart.Clear();

            FinalShowCount = 0;

            if(!isgyh)
            {
                if (DataFre1.Count > 0 && DataFre1.Count == DataFre2.Count)
                {
                    for (int i = 0; i < DataFre1.Count; i++)
                    {
                        if (DataFre2[i] >= DataFre1[i])
                        {
                            JudgeResult.Add(1);
                        }
                        else
                        {
                            JudgeResult.Add(0);
                        }
                    }
                }
            }
            else
            {
                

                if (DataFre1.Count > 0 && DataFre1.Count == DataFre2.Count)
                {
                    double min1 = DataFre1.Min();
                    double max1 = DataFre1.Max();
                    double min2 = DataFre2.Min();
                    double max2 = DataFre2.Max();
                    double dis1 = max1 - min1;
                    double dis2 = max2 - min2;

                    for (int i = 0; i < DataFre1.Count; i++)
                    {
                        double tmp2 = (DataFre2[i] - min2) / dis2;
                        double tmp1 = (DataFre1[i] - min1) / dis1;

                        if (tmp2 >= tmp1)
                        {
                            JudgeResult.Add(1);
                        }
                        else
                        {
                            JudgeResult.Add(0);
                        }
                    }
                }
            }

            int index = shift + discnt/2;
            int TN = JudgeResult.Count;
            while(index<TN)
            {
                FinalResult.Add(JudgeResult[index]);

                JudgePoints.Add(index, JudgeResult[index]);

                if(index<showcnt)
                {
                    FinalShowCount++;
                }

                index += discnt;
            }
            

            for(int i=0;i<FinalResult.Count;i++)
            {
                if(i<FinalShowCount)
                {
                    JudgeShowPointsPart.Add(new int[2] { i, FinalResult[i] });
                    JudgeShowPointsPart.Add(new int[2] { i + 1, FinalResult[i] });
                }

                JudgeShowPointsFull.Add(new int[2] { i, FinalResult[i] });
                JudgeShowPointsFull.Add(new int[2] { i + 1, FinalResult[i] });
            }
        }

        public void JudgementASK(int shift, int discnt,int showcnt)
        {
            JudgeResult.Clear();

            FinalResult.Clear();

            JudgePoints.Clear();

            JudgeShowPointsFull.Clear();
            JudgeShowPointsPart.Clear();

            FinalShowCount = 0;

            if (DataFre1.Count > 0)
            {
                double min1 = DataFre1.Min();
                double max1 = DataFre1.Max();

                double mid1 = (min1 + max1) / 2;

                for (int i = 0; i < DataFre1.Count; i++)
                {
                    
                    if (DataFre1[i] >= mid1)
                    {
                        JudgeResult.Add(1);
                    }
                    else
                    {
                        JudgeResult.Add(0);
                    }
                }
            }

            int index = shift + discnt / 2;
            int TN = JudgeResult.Count;
            while (index < TN)
            {
                FinalResult.Add(JudgeResult[index]);

                JudgePoints.Add(index, JudgeResult[index]);

                index += discnt;
            }


            for (int i = 0; i < FinalResult.Count; i++)
            {
                //if (i < FinalShowCount)
                //{
                //    JudgeShowPointsPart.Add(new int[2] { i, FinalResult[i] });
                //    JudgeShowPointsPart.Add(new int[2] { i + 1, FinalResult[i] });
                //}

                JudgeShowPointsFull.Add(new int[2] { i, FinalResult[i] });
                JudgeShowPointsFull.Add(new int[2] { i + 1, FinalResult[i] });
            }
        }

        public void Judgement(int shift, int discnt, bool isgyh,ref MJietiaoShowResultFSK res)
        {

            JudgeResult.Clear();
            FinalResult.Clear();

            if (!isgyh)
            {
                if (DataFre1.Count > 0 && DataFre1.Count == DataFre2.Count)
                {
                    for (int i = 0; i < DataFre1.Count; i++)
                    {
                        if (DataFre2[i] >= DataFre1[i])
                        {
                            JudgeResult.Add(1);
                        }
                        else
                        {
                            JudgeResult.Add(0);
                        }
                    }
                }
            }
            else
            {


                if (DataFre1.Count > 0 && DataFre1.Count == DataFre2.Count)
                {
                    double min1 = DataFre1.Min();
                    double max1 = DataFre1.Max();
                    double min2 = DataFre2.Min();
                    double max2 = DataFre2.Max();
                    double dis1 = max1 - min1;
                    double dis2 = max2 - min2;

                    for (int i = 0; i < DataFre1.Count; i++)
                    {
                        double tmp2 = (DataFre2[i] - min2) / dis2;
                        double tmp1 = (DataFre1[i] - min1) / dis1;

                        if (tmp2 >= tmp1)
                        {
                            JudgeResult.Add(1);
                        }
                        else
                        {
                            JudgeResult.Add(0);
                        }
                    }
                }
            }

            res.Judge_Y = JudgeResult;

            int index = shift + discnt / 2;
            int TN = JudgeResult.Count;
            while (index < TN)
            {
                FinalResult.Add(JudgeResult[index]);

                JudgePoints.Add(index, JudgeResult[index]);

                res.ChouYang_X.Add(index);
                res.ChouYang_Y.Add(JudgeResult[index]);

                index += discnt;
            }

            res.JudgeResult = FinalResult;

            //for (int i = 0; i < FinalResult.Count; i++)
            //{
            //    if (i < FinalShowCount)
            //    {
            //        JudgeShowPointsPart.Add(new int[2] { i, FinalResult[i] });
            //        JudgeShowPointsPart.Add(new int[2] { i + 1, FinalResult[i] });
            //    }

            //    JudgeShowPointsFull.Add(new int[2] { i, FinalResult[i] });
            //    JudgeShowPointsFull.Add(new int[2] { i + 1, FinalResult[i] });
            //}
        }

        public void Judgement2(int shift, int discnt, bool isgyh, ref MJietiaoShowResultFSK res)
        {

            JudgeResult.Clear();
            FinalResult.Clear();

            if (!isgyh)
            {
                if (DataFre1.Count > 0 && DataFre1.Count == DataFre2.Count)
                {
                    for (int i = 0; i < DataFre1.Count; i++)
                    {
                        if (DataFre2[i] >= DataFre1[i])
                        {
                            JudgeResult.Add(1);
                        }
                        else
                        {
                            JudgeResult.Add(0);
                        }
                    }
                }
            }
            else
            {


                if (DataFre1.Count > 0 && DataFre1.Count == DataFre2.Count)
                {
                    double min1 = DataFre1.Min();
                    double max1 = DataFre1.Max();
                    double min2 = DataFre2.Min();
                    double max2 = DataFre2.Max();
                    double dis1 = max1 - min1;
                    double dis2 = max2 - min2;

                    for (int i = 0; i < DataFre1.Count; i++)
                    {
                        double tmp2 = (DataFre2[i] - min2) / dis2;
                        double tmp1 = (DataFre1[i] - min1) / dis1;

                        if (tmp2 >= tmp1)
                        {
                            JudgeResult.Add(1);
                        }
                        else
                        {
                            JudgeResult.Add(0);
                        }
                    }
                }
            }

            res.Judge_Y = JudgeResult;

            int index = shift + discnt / 2;
            int TN = JudgeResult.Count;
            while (index < TN)
            {
                FinalResult.Add(JudgeResult[index]);

                JudgePoints.Add(index, JudgeResult[index]);

                res.ChouYang_X.Add(index);
                res.ChouYang_Y.Add(JudgeResult[index]);

                index += discnt;
            }

            res.JudgeResult = FinalResult;

            //for (int i = 0; i < FinalResult.Count; i++)
            //{
            //    if (i < FinalShowCount)
            //    {
            //        JudgeShowPointsPart.Add(new int[2] { i, FinalResult[i] });
            //        JudgeShowPointsPart.Add(new int[2] { i + 1, FinalResult[i] });
            //    }

            //    JudgeShowPointsFull.Add(new int[2] { i, FinalResult[i] });
            //    JudgeShowPointsFull.Add(new int[2] { i + 1, FinalResult[i] });
            //}
        }

        public string GetFinalResult()
        {
            string res = "";
            foreach (int v in FinalResult)
            {
                res += v.ToString();
            }


            return res;
            
        }

        public string GetSampleFre()
        {
            if(SampleFre>=1000)
            {
                double tmp = SampleFre * 0.001;

                return tmp.ToString("F3").TrimEnd('0').TrimEnd('.') + "k";
            }
            else
            {
                return SampleFre.ToString();
            }
        }

        public void CalSampleFre()
        {
            if (XValues.Count > 1)
            {
                SampleFre=(int)(1.0 / (XValues[1] - XValues[0]));
            }
            else
            {
                SampleFre = 0;
            }
            
        }

        public bool HasDataFre()
        {
            return DataFre1.Count > 0 && DataFre2.Count > 0;
        }

        
    }
}
